package mo.org.cm309.shoppingcart.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.util.http.fileupload.FileUploadException;

import mo.org.cm309.exception.UploadTypeNotAllowException;
import mo.org.cm309.shoppingcart.entity.Product;
import mo.org.cm309.shoppingcart.service.ProductService;
import mo.org.cm309.shoppingcart.utility.CommonUtility;

public class ProductEditServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		ProductService productService = new ProductService();
		long id = CommonUtility.getLongByString(req.getParameter("productId"));
		Product product = productService.getProductById(id);
		if (product != null){
			req.setAttribute("product", product);
			req.getRequestDispatcher("/productEdit.jsp").forward(req, resp);
		}else{
			PrintWriter writer = resp.getWriter();
			writer.print("<html>");
			writer.print("Error 404 : Product Not Found !");
			writer.print("</html>");
		}
	}
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try{
			req.setCharacterEncoding("UTF-8");
			HashMap map = CommonUtility.getElementByRequest(req);
			String id 	= String.valueOf(map.get("id"));
			String productname = String.valueOf(map.get("productName"));
			String category    = String.valueOf(map.get("category"));
			String brand       = String.valueOf(map.get("brand"));
			String supplier    = String.valueOf(map.get("supplier"));
			String spec1       = String.valueOf(map.get("spec1"));
			String spec2  	   = String.valueOf(map.get("spec2"));
			String spec3  	   = String.valueOf(map.get("spec3"));
			String spec4       = String.valueOf(map.get("spec4"));
			String spec5       = String.valueOf(map.get("spec5"));
			String spec6       = String.valueOf(map.get("spec6"));
			String spec7       = String.valueOf(map.get("spec7"));
			String spec8       = String.valueOf(map.get("spec8"));
			String spec9       = String.valueOf(map.get("spec9"));
			String spec10      = String.valueOf(map.get("spec10"));
			String desc        = String.valueOf(map.get("desc"));
			double price       = CommonUtility.getDoubleByString(String.valueOf(map.get("price")));
			double discount    = CommonUtility.getDoubleByString(String.valueOf(map.get("discount")));
			int stock   	   = CommonUtility.getIntByString(String.valueOf(map.get("stock")));
			int sold 		   = CommonUtility.getIntByString(String.valueOf(map.get("sold")));
			byte[] image       = (byte[])map.get("image");
			Product product = new Product();
			product.setId(CommonUtility.getLongByString(id));
			product.setProductname(productname);
			product.setCategory(category);
			product.setBrand(brand);
			product.setSupplier(supplier);
			product.setSpec1(spec1);
			product.setSpec2(spec2);
			product.setSpec3(spec3);
			product.setSpec4(spec4);
			product.setSpec5(spec5);
			product.setSpec6(spec6);
			product.setSpec7(spec7);
			product.setSpec8(spec8);
			product.setSpec9(spec9);
			product.setSpec10(spec10);
			product.setDesc(desc);
			product.setPrice(price);
			product.setDiscount(discount);
			product.setStock(stock);
			product.setSold(sold);
			product.setImage(image);
			ProductService productService = new ProductService();
			productService.update(product);
		} catch (UploadTypeNotAllowException e){
			e.printStackTrace();
		} catch (FileUploadException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
